package com.xueqiu.movieplaymanagersystem.controller.admin;

import com.xueqiu.movieplaymanagersystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.xueqiu.movieplaymanagersystem.entity.Blog;

import java.util.List;

/**
 * @author xueqiu
 */
@Controller
public class AdminIndexController {
    @Autowired
    private TypesService typesService;
    @Autowired
    private TagService tagService;
    /*
    typesService和tagService
    这两个成员变量
    只能直接用对应的，TagService接口，TypeService接口，进行多态实例化
    不能用PublicService接口实例化，因为PublicService接口方法比TagService接口少。
     */
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private VisitService visitService;

    /**
     * 前置跳转控制器；跳转到主页
     * @param model 模型参数
     * @return 主页
     */
    @GetMapping("/admin/index.html")
    public String count(Model model) {
        //查询当前周访问量
        int weekCount = visitService.findWeekCount();
        //查询截至目前为止所有访问量
        int allCount = visitService.findCount();
        model.addAttribute("week", weekCount);
        model.addAttribute("total", allCount);

        //查询全部类别的数量
        Integer typesCount = typesService.findCount();
        model.addAttribute("types", typesCount);

        //查询全部标签的数量
        Integer tagCount = tagService.findCount();
        model.addAttribute("tags", tagCount);

        //查询所有新闻数量，包含保存的新闻和发布的新闻
        Integer blogCount = blogService.findCount();
        model.addAttribute("allBlog", blogCount);

        //查询所有已发布的新闻的数量
        int count = blogService.findPublishCount();
        model.addAttribute("publishBlog", count);

        //查询所有保存的新闻数量
        Integer saveBlogCount = blogService.findSaveCount();
        model.addAttribute("saveBlog", saveBlogCount);

        //查询数据库中所有评论条数
        Integer commentCount = commentService.findCount();
        model.addAttribute("comment", commentCount);

        //查询访问量最多的1个新闻
        Blog blog = blogService.findTopOneViews();
        model.addAttribute("blog", blog);

        //查询点赞量优先的最多点赞数的前6名新闻
        List<Blog> hotBlog = blogService.findTopSixLikes();
        model.addAttribute("hot", hotBlog);

        return "admin/homepage2";
    }
}
